package com.google.zxing.client.android.history;

import java.io.File;
import java.util.Calendar;

import com.google.zxing.client.android.R;
import com.google.zxing.client.android.R.layout;
import com.google.zxing.client.android.R.menu;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker;

public class DateAndTimeActivity extends Activity {

	private int yyyy,mm,dd,hh,m;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_date_and_time);
		
		
		Calendar c = Calendar.getInstance();
		final DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
		datePicker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), new OnDateChangedListener() {
			
			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				// TODO Auto-generated method stub
			}
		}); 
		
		final TimePicker timePicker = (TimePicker) findViewById(R.id.timePicker1);
		timePicker.setIs24HourView(true);
		timePicker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
		
		
		final Button button = (Button) findViewById(R.id.envoyer);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	
                yyyy = datePicker.getYear();
                mm = datePicker.getMonth();
                dd = datePicker.getDayOfMonth();
                hh = timePicker.getCurrentHour();
                m = timePicker.getCurrentMinute();
                
                String dateTime = formatDateString(yyyy, mm, dd, hh, m);
                
                SQLiteOpenHelper helper = new DBHelper(v.getContext());
          	  	SQLiteDatabase db = null;
          	  	
          	  	try{
          	  		db = helper.getWritableDatabase();
          	  	}catch(SQLiteException e){
          	  		Log.i(getResources().getString(R.string.orasuo),e.toString());
          	  	}
          	  	
          	  	//write data to disk
          	  	DatabaseAssistant dbAs = new DatabaseAssistant(v.getContext(), db , createFilePath(dateTime));
          	  	dbAs.exportData();
          	  	
          	  	//Send the data via email
				  Intent email = new Intent(Intent.ACTION_SEND);
				  email.putExtra(Intent.EXTRA_EMAIL, new String[]{"votreadress@univ-savoie.fr"});		  
				  email.putExtra(Intent.EXTRA_SUBJECT, dateTime);
				  email.putExtra(Intent.EXTRA_TEXT, dbAs.readTextFromFile());
				  email.setType("message/rfc822");
				  startActivity(Intent.createChooser(email, "Choisir une Email client :"));

            }
        });
	}

	public String createFilePath(String dateTime){
		File file = Environment.getExternalStorageDirectory();
		dateTime = file.toString() + "/" + dateTime;
		dateTime = dateTime.concat(".xml");
		return dateTime;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_date_and_time, menu);
		return true;
	}
	
	public String formatDateString(int yyyy,int mm,int dd,int hh,int m){
		String minutes = Integer.toString(m);
		if(minutes.length() == 1){
			minutes = "0".concat(minutes);
		}
		return dd + "_" + (mm+1) + "_" + yyyy + "_" + hh + "_" + minutes;
	}
	

}
